import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: leetcode.中等题.逆波兰表达式求值
 */
public class Test {

    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String x: tokens) {
            if(!IsOperators(x)) {
                stack.push(Integer.parseInt(x));
                continue;
            }
            int num1 = 0;
            int num2 = 0;
            switch (x){
                case "+":
                    num2 = stack.pop();
                    num1 = stack.pop();
                    stack.push(num1 + num2);
                    break;
                case "-":
                    num2 = stack.pop();
                    num1 = stack.pop();
                    stack.push(num1 - num2);
                    break;
                case "*":
                    num2 = stack.pop();
                    num1 = stack.pop();
                    stack.push(num1 * num2);
                    break;
                case "/":
                    num2 = stack.pop();
                    num1 = stack.pop();
                    stack.push(num1 / num2);
                    break;
            }
        }
        return stack.pop();
    }

    //警告：字符串比较尽量或者绝对不要用 == 来判断，比较的是地址
    public static boolean IsOperators(String s) {
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String[] s = new String[]{"2","1","+","3","*"};
        int ret = evalRPN(s);
    }
}
